package com.zzvcom.webapp.action.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.News;
import com.zzvcom.service.admin.NewsManager;
import com.zzvcom.template.ResultPage;

@Controller
@Scope("prototype")
// 声明此类为控制层的类,且为prototype/singleton模式调用
public class NewsViewAction
{
    private NewsManager nm;

    private ResultPage resultPage;

    private News news;

    private String param;

    private DetachedCriteria helper()
    {
        DetachedCriteria dc = DetachedCriteria.forClass(News.class);
        dc.addOrder(Order.desc("seq"));       
        return dc;
    }
    
    public void queryByType(String ntype)
    {
        DetachedCriteria dc=helper();
        dc.add(Restrictions.eq("ntype", ntype));
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        if (StringUtils.isNotBlank(param))
        {
            resultPage.setCurrentPage(Integer.valueOf(param));
        }       
        resultPage.setDetachedCriteria(dc);
        nm.getResultPageByDetachedCriteria(resultPage);
    }

    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        if (StringUtils.isNotBlank(param))
        {
            resultPage.setCurrentPage(Integer.valueOf(param));
        }
        DetachedCriteria dc = helper();
        resultPage.setDetachedCriteria(dc);
        nm.getResultPageByDetachedCriteria(resultPage);
        return "news";
    }
    
    private String ntype;
    
    public String customer()
    {
        ntype="1";
        queryByType(ntype);
        return "news";
    }
    
    public String knowledge ()
    {
        ntype="2";
        queryByType(ntype);
        return "news";
    }

    public String p()
    {
        return execute();
    }

    private News preNews;

    private News nextNews;

    public String a()
    {
        news = nm.get(param);
        if(news!=null)
        {
            //prev
            DetachedCriteria preDC = DetachedCriteria.forClass(News.class);            
            preDC.add(Restrictions.eq("ntype", news.getNtype()));
            preDC.add(Restrictions.ge("seq", news.getSeq()));             
            preDC.add(Restrictions.ne("id", news.getId())); 
            preDC.addOrder(Order.desc("seq"));           
            List<News> list = nm.getListByCriteria(preDC, 0, 1);
            if (list.size() > 0)
            {
                preNews = list.get(0);
            }
            //next
            DetachedCriteria nextDC = DetachedCriteria.forClass(News.class);    
            nextDC.add(Restrictions.eq("ntype", news.getNtype())); 
            nextDC.add(Restrictions.le("seq", news.getSeq()));
            nextDC.add(Restrictions.ne("id", news.getId())); 
            nextDC.addOrder(Order.desc("seq"));           
            list = nm.getListByCriteria(nextDC, 0, 1);
            if (list.size() > 0)
            {
                nextNews = list.get(0);
            }
        }
        return "anews";
    }

    public NewsManager getNm()
    {
        return nm;
    }

    @Autowired
    public void setNm(NewsManager nm)
    {
        this.nm = nm;
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public News getNews()
    {
        return news;
    }

    public void setNews(News news)
    {
        this.news = news;
    }

    public String getParam()
    {
        return param;
    }

    public void setParam(String param)
    {
        this.param = param;
    }

    public News getPreNews()
    {
        return preNews;
    }

    public void setPreNews(News preNews)
    {
        this.preNews = preNews;
    }

    public News getNextNews()
    {
        return nextNews;
    }

    public void setNextNews(News nextNews)
    {
        this.nextNews = nextNews;
    }

    public String getNtype()
    {
        return ntype;
    }

    public void setNtype(String ntype)
    {
        this.ntype = ntype;
    }

}
